//
//  MySearchViewController.m
//  LastOne
//
//  Created by 小柠檬 on 15/11/11.
//  Copyright © 2015年 sc. All rights reserved.
//

#import "MySearchViewController.h"
#import "FitHeader.h"
#import "CoreDataManager.h"
#import "NSString+KeywordHighLight.h"
#import "HQHListDetailTableViewCell.h"
#import "NightHeader.h"
#import "HQHListDetailWebViewController.h"
@interface MySearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UILabel *flagLabel;
@property(nonatomic, strong)UISearchBar *searchBar;
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSString *searchText;
@property(nonatomic, strong)NSArray *dataSource;
@property(nonatomic, strong)CoreDataManager *manager;

@end

@implementation MySearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    
    self.dataSource = [NSArray array];
    
    self.searchBar = [[UISearchBar alloc]init];
    _searchBar.delegate = self;
    [_searchBar setBarStyle:0];
    [_searchBar setTranslucent:YES];
    _searchBar.backgroundColor = [UIColor clearColor];
    _searchBar.placeholder = @"请输入您要搜索的...";
    _manager = [CoreDataManager shareManager];
    self.navigationItem.titleView = _searchBar;
 
    

}

-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    self.searchText = [searchText copy];
    
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"HQHReadSave"];
    //查询的条件
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"title CONTAINS%@", _searchText];
   

    fetchRequest.predicate = predicate;
  
    
    //将请求对象发给manager
    _dataSource = [_manager.managedObjectContext executeFetchRequest:fetchRequest error:nil];
  
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375 * FitWidth, 494 * FitHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    //初始化尾视图
    _tableView.tableFooterView = [[UITableView alloc]init];
    [_tableView reloadData];
    
    [searchBar resignFirstResponder];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
   
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 160 * FitHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    HQHListDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[HQHListDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.listDetailModel = _dataSource[indexPath.row];
    cell.attributedText = _searchText;
  
    [cell NightWithType:UIViewColorTypeNormal];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    HQHListDetailWebViewController *web = [[HQHListDetailWebViewController alloc]init];
    web.model = _dataSource[indexPath.row];
    [self.navigationController pushViewController:web animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
